GtkHeaderBar: try harder to find the best window icon
authorMatthias Clasen <mclasen@redhat.com>
Fri, 24 Jan 2014 20:40:37 +0000 (15:40 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 24 Jan 2014 20:40:37 +0000 (15:40 -0500)
GtkWindow has 4 (!) APIs for setting window icons, and we
have to try them all in the right order to find the right
icon. This commit makes it so, and keeps the icon list
manipulation inside gtkwindow.c by adding a private API
for getting a single icon at the right size.

https://bugzilla.gnome.org/show_bug.cgi?id=722515

gtk/gtkheaderbar.c
gtk/gtkwindow.c
gtk/gtkwindowprivate.h

index 3b9923eea241ebceacd4275118b53744f36e4e26..36c7e7008140978cfe30c15ed2b819bd6b1834a8 100644 (file)
@@ -218,54 +218,20 @@ _gtk_header_bar_update_window_icon (GtkHeaderBar *bar,
                                     GtkWindow    *window)
 {
   GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
-  gint size;
-  GList *list;
-  const gchar *name;
-  GdkPixbuf *best = NULL;
+  GdkPixbuf *pixbuf;
 
   if (priv->titlebar_icon == NULL)
     return FALSE;
 
   if (GTK_IS_BUTTON (gtk_widget_get_parent (priv->titlebar_icon)))
-    size = 16;
+    pixbuf = gtk_window_get_icon_for_size (window, 16);
   else
-    size = 20;
+    pixbuf = gtk_window_get_icon_for_size (window, 20);
 
-  list = gtk_window_get_icon_list (window);
-
-  if (list != NULL)
-    {
-      GdkPixbuf *pixbuf;
-      GList *l;
-
-      best = NULL;
-      for (l = list; l; l = l->next)
-        {
-          pixbuf = list->data;
-          if (gdk_pixbuf_get_width (pixbuf) <= size)
-            {
-              best = g_object_ref (pixbuf);
-              break;
-            }
-        }
-
-      if (best == NULL)
-        best = gdk_pixbuf_scale_simple (GDK_PIXBUF (list->data), size, size, GDK_INTERP_BILINEAR);
-
-      g_list_free (list);
-    }
-  else
-    {
-      name = gtk_window_get_icon_name (window);
-      if (name != NULL)
-        best = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
-                                         name, size, GTK_ICON_LOOKUP_FORCE_SIZE, NULL);
-    }
-
-  if (best)
+  if (pixbuf)
     {
-      gtk_image_set_from_pixbuf (GTK_IMAGE (priv->titlebar_icon), best);
-      g_object_unref (best);
+      gtk_image_set_from_pixbuf (GTK_IMAGE (priv->titlebar_icon), pixbuf);
+      g_object_unref (pixbuf);
       gtk_widget_show (priv->titlebar_icon);
 
       return TRUE;
index f42f9f76a9618fb7fc6adcb1e11d0f13ddafd987..5e2cc05aee45bfc5d0bf618fd86844605165ab44 100644 (file)
@@ -4019,6 +4019,73 @@ gtk_window_realize_icon (GtkWindow *window)
     }
 }
 
+static GdkPixbuf *
+icon_from_list (GList *list,
+                gint   size)
+{
+  GdkPixbuf *best;
+  GdkPixbuf *pixbuf;
+  GList *l;
+
+  best = NULL;
+  for (l = list; l; l = l->next)
+    {
+      pixbuf = list->data;
+      if (gdk_pixbuf_get_width (pixbuf) <= size)
+        {
+          best = g_object_ref (pixbuf);
+          break;
+        }
+    }
+
+  if (best == NULL)
+    best = gdk_pixbuf_scale_simple (GDK_PIXBUF (list->data), size, size, GDK_INTERP_BILINEAR);
+
+  return best;
+}
+
+static GdkPixbuf *
+icon_from_name (const gchar *name,
+                gint         size)
+{
+  return gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+                                   name, size,
+                                   GTK_ICON_LOOKUP_FORCE_SIZE, NULL);
+}
+
+GdkPixbuf *
+gtk_window_get_icon_for_size (GtkWindow *window,
+                              gint       size)
+{
+  GtkWindowPrivate *priv = window->priv;
+  GtkWindowIconInfo *info;
+  const gchar *name;
+
+  info = ensure_icon_info (window);
+
+  if (info->icon_list != NULL)
+    return icon_from_list (info->icon_list, size);
+
+  name = gtk_window_get_icon_name (window);
+  if (name != NULL)
+    return icon_from_name (name, size);
+
+  if (priv->transient_parent != NULL)
+    {
+      info = ensure_icon_info (priv->transient_parent);
+      if (info->icon_list)
+        return icon_from_list (info->icon_list, size);
+    }
+
+  if (default_icon_list != NULL)
+    return icon_from_list (default_icon_list, size);
+
+  if (default_icon_name != NULL)
+    return icon_from_name (default_icon_name, size);
+
+  return NULL;
+}
+
 static void
 gtk_window_unrealize_icon (GtkWindow *window)
 {
index 9f9bce6bd0da23d35072473a040fd7fc374bfede..704026d0010acadd2ed09e6d67152e9d9ac5d8f6 100644 (file)
@@ -107,6 +107,9 @@ void    _gtk_window_get_popover_position (GtkWindow                   *window,
                                           GtkPositionType             *pos,
                                           cairo_rectangle_int_t       *rect);
 
+GdkPixbuf *gtk_window_get_icon_for_size (GtkWindow *window,
+                                         gint       size);
+
 G_END_DECLS
 
 #endif /* __GTK_WINDOW_PRIVATE_H__ */